package com.wsd.jing.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsd.jing.entity.Post;
import com.wsd.jing.entity.UserPost;
import com.wsd.jing.enums.CodeEnum;
import com.wsd.jing.exception.BusinessException;
import com.wsd.jing.mapper.PostMapper;
import com.wsd.jing.mapper.UserPostMapper;
import com.wsd.jing.request.CommonRequest;
import com.wsd.jing.service.IPostService;
import com.wsd.jing.utils.SortUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * All rights Reserved, Designed By www.tom.com
 *
 * @author tom
 * @version 1.0.0
 * @projectName: jing
 * @packageName: com.wsd.jing.service.impl
 * @className: PostServiceImpl
 * @description: 职位服务实现类
 * @createDate 2022-11-20 07:41
 * @copyright 2022-11-20 07:41 www.tom.com Inc. All rights reserved.
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IPostService {

    private final UserPostMapper userPostMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertPost(Post post) {
        QueryWrapper<Post> postQueryWrapper = new QueryWrapper<>();
        postQueryWrapper.lambda().eq(Post::getCode, post.getCode());
        Post post1 = this.getOne(postQueryWrapper);
        if (ObjectUtil.isNotEmpty(post1)) {
            throw new BusinessException(CodeEnum.POST_NAME_EXIST);
        }
        post.setCreateTime(LocalDateTime.now());
        return this.save(post);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updatePost(Post post) {
        QueryWrapper<Post> postQueryWrapper = new QueryWrapper<>();
        postQueryWrapper.lambda().eq(Post::getCode, post.getCode());
        Post post1 = this.getOne(postQueryWrapper);
        if (ObjectUtil.isNotEmpty(post1) && !post.getId().equals(post1.getId())) {
            throw new BusinessException(CodeEnum.POST_NAME_EXIST);
        }
        post.setUpdateTime(LocalDateTime.now());
        return this.updateById(post);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deletePost(Integer id) {
        QueryWrapper<UserPost> userPostQueryWrapper = new QueryWrapper<>();
        userPostQueryWrapper.lambda().eq(UserPost::getPostId, id);
        List<UserPost> userPostList = userPostMapper.selectList(userPostQueryWrapper);
        if (CollectionUtil.isNotEmpty(userPostList)) {
            throw new BusinessException(CodeEnum.POST_NOT_DELETE);
        }
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deletePosts(String ids) {
        List<String> idList = StrUtil.splitTrim(ids, ",");
        QueryWrapper<UserPost> userPostQueryWrapper = new QueryWrapper<>();
        userPostQueryWrapper.lambda().in(UserPost::getPostId, idList);
        List<UserPost> userPostList = userPostMapper.selectList(userPostQueryWrapper);
        if (CollectionUtil.isNotEmpty(userPostList)) {
            throw new BusinessException(CodeEnum.POST_NOT_DELETE);
        }
        return this.removeByIds(idList);
    }

    @Override
    public Post getPost(Integer id) {
        return this.getById(id);
    }

    @Override
    public List<Post> listPosts(CommonRequest commonRequest, Post post) {
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        // TODO 设置查询条件
        if (ObjectUtil.isNotNull(post.getStatus())) {
            queryWrapper.lambda().eq(Post::getStatus, post.getStatus());
        }
        // 处理排序字段
        SortUtil.handleListSort(commonRequest, queryWrapper);
        return this.list(queryWrapper);
    }

    @Override
    public Map<String, Object> listPagePosts(CommonRequest commonRequest, Post post) {
        Map<String, Object> resultMap = new HashMap<>(2);
        Page<Post> page = new Page<>(commonRequest.getPageNum(), commonRequest.getPageSize());
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        // TODO 设置查询条件
        if (StrUtil.isNotBlank(post.getName())) {
            queryWrapper.lambda().like(Post::getName, post.getName());
        }
        if (StrUtil.isNotBlank(post.getCode())) {
            queryWrapper.lambda().like(Post::getCode, post.getCode());
        }

        // 处理排序字段
        SortUtil.handlePageSort(commonRequest, page);
        IPage<Post> result = this.page(page, queryWrapper);
        resultMap.put("rows", result.getRecords());
        resultMap.put("total", result.getTotal());
        return resultMap;
    }
}
